import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { resolve } from 'path';
import type { ConfigEnv, UserConfig } from 'vite';
import { defineConfig, loadEnv } from 'vite';

const envPrefix = ['VITE', 'VUE'];
export default defineConfig(({ mode }: ConfigEnv) => {
  const envVars = loadEnv(mode, process.cwd(), envPrefix);

  const config: UserConfig = {
    define: {
      'process.env': envVars,
    },
    plugins: [vue(), vueJsx()],
    base: './',
    resolve: {
      alias: [{ find: '@', replacement: resolve(__dirname, 'src') }],
    },
    envPrefix: envPrefix,
    server: {
      port: envVars.VUE_APP_PORT ? Number(envVars.VUE_APP_PORT) : 8101,
      proxy: {
        '/system-manage': {
          target: 'http://localhost:9011',
          changeOrigin: true,
          // rewrite: path => path.replace(/^\/system-manage/, ''), // 重写路径
        },
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          typescriptEnabled: true,
        },
      },
    },
    build: {
      rollupOptions: {
        output: {
          entryFileNames: `assets/[name].[hash].js`,
          chunkFileNames: `assets/[name].[hash].js`,
          assetFileNames: `assets/[name].[hash].[ext]`,
        },
      },
    },
  };
  return config;
});
